﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Data;

namespace YY.UI
{
    #region ExcelCommon 操作类
    /// <SUMMARY>
    /// ExcelEdit 的摘要说明 //引入Excel的COM组件
    /// </SUMMARY>
    public class ExcelCommon : IDisposable
    {

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out   int ID);

        public string mFilename;
        public Excel.Application app;
        public Excel.Workbooks wbs;
        public Excel.Workbook wb;
        public Excel.Worksheets wss;
        public Excel.Worksheet ws;
        public Excel.Range rng;
        public ExcelCommon()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 创建一个Excel对象
        /// </summary>
        public void Create()
        {
            app = new Excel.Application();
            wbs = app.Workbooks;
            wb = wbs.Add(true);
        }

        /// <summary>
        /// 打开一个Excel文件
        /// </summary>
        /// <param name="FileName"></param>
        public void Open(string FileName)
        {
            app = new Excel.Application();
            wbs = app.Workbooks;
            wb = wbs.Add(FileName);
            //wb = wbs.Open(FileName, 0, true, 5,"", "", true, Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);
            //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
            mFilename = FileName;
        }

        /// <summary>
        /// 获取一个工作表
        /// </summary>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet GetSheet(string SheetName)
        {
            Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[SheetName];
            return s;
        }

        /// <summary>
        /// 添加一个工作表
        /// </summary>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet AddSheet(string SheetName)
        {
            Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            s.Name = SheetName;
            return s;
        }

        /// <summary>
        /// 删除一个工作表
        /// </summary>
        /// <param name="SheetName"></param>
        public void DelSheet(string SheetName)
        {
            ((Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
        }

        /// <summary>
        /// 重命名一个工作表
        /// </summary>
        /// <param name="OldSheetName"></param>
        /// <param name="NewSheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)
        {
            Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[OldSheetName];
            s.Name = NewSheetName;
            return s;
        }

        /// <summary>
        /// 重命名一个工作表
        /// </summary>
        /// <param name="Sheet"></param>
        /// <param name="NewSheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet ReNameSheet(Excel.Worksheet Sheet, string NewSheetName)
        {

            Sheet.Name = NewSheetName;

            return Sheet;
        }

        /// <summary>
        /// 要设值的工作表的名称 X行Y列 value 值
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void SetCellValue(Excel.Worksheet ws, int x, int y, object value)
        {
            ws.Cells[x, y] = value;
        }

        /// <summary>
        /// 要设值的工作表的名称 X行Y列 value 值
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void SetCellValue(string ws, int x, int y, object value)
        {

            GetSheet(ws).Cells[x, y] = value;
        }

        /// <summary>
        /// 设置一个单元格的属性 字体，粗体，大小，颜色，对齐方式
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="Startx"></param>
        /// <param name="Starty"></param>
        /// <param name="Endx"></param>
        /// <param name="Endy"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="color"></param>
        /// <param name="HorizontalAlignment"></param>
        public void SetCellProperty(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, bool bold, string name, Excel.Constants color, Excel.Constants HorizontalAlignment, bool wrapText)
        {
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Bold = bold;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).WrapText = wrapText;             
        }


        /// <summary>
        /// 设置一个单元格的属性 字体，大小，颜色，对齐方式
        /// </summary>
        /// <param name="wsn"></param>
        /// <param name="Startx"></param>
        /// <param name="Starty"></param>
        /// <param name="Endx"></param>
        /// <param name="Endy"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="color"></param>
        /// <param name="HorizontalAlignment"></param>
        public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)
        {
            //name = "宋体";
            //size = 12;
            //color = Excel.Constants.xlAutomatic;
            //HorizontalAlignment = Excel.Constants.xlRight;

            Excel.Worksheet ws = GetSheet(wsn);
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;

            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
        }

        /// <summary>
        /// 设置区域网格
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="Startx"></param>
        /// <param name="Starty"></param>
        /// <param name="Endx"></param>
        /// <param name="Endy"></param>
        /// <param name="lineSyle"></param>
        /// <param name="borderWeight"></param>
        /// <param name="colorIndex"></param>
        public void SetRangeGrid(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, Excel.XlLineStyle lineSyle, Excel.XlBorderWeight borderWeight, Excel.XlColorIndex colorIndex)
        {
            rng = ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]);
            rng.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            rng.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone;

            rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeTop].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = colorIndex;
        }

        /// <summary>
        /// 指定单元格宽度自动适应
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public void ColumnsAutoFit(Excel.Worksheet ws, int x1, int y1, int x2, int y2)
        {
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Select();
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Columns.AutoFit();
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Rows.AutoFit();
        }

        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public void UniteCells(Excel.Worksheet ws, int x1, int y1, int x2, int y2)
        {
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
        }


        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public void UniteCells(string ws, int x1, int y1, int x2, int y2)
        {
            GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);

        }


        /// <summary>
        /// 将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
        {

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    GetSheet(ws).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();

                }

            }

        }

        /// <summary>
        /// 将内存中数据表格插入到Excel指定工作表的指定位置二
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void InsertTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY)
        {

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {

                    ws.Cells[startX + i, j + startY] = dt.Rows[i][j];

                }

            }

        }


        /// <summary>
        /// 将内存中数据表格添加到Excel指定工作表的指定位置一
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
        {

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {

                    GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];

                }

            }

        }

        /// <summary>
        /// 将内存中数据表格添加到Excel指定工作表的指定位置二
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void AddTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY)
        {


            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {

                    ws.Cells[i + startX, j + startY] = dt.Rows[i][j];

                }
            }

        }

        /// <summary>
        /// 插入图片操作
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="ws"></param>
        public void InsertPictures(string Filename, string ws)
        {
            //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
            //后面的数字表示位置
        }

        /// <summary>
        /// 插入图片操作
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="ws"></param>
        /// <param name="Height"></param>
        /// <param name="Width"></param>
        public void InsertPictures(string Filename, string ws, int Height, int Width)
        {
            //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
        }

        /// <summary>
        /// 插入图片操作
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="ws"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="Height"></param>
        /// <param name="Width"></param>
        public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)
        {

            //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
        }

        public void InsertActiveChart(Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Excel.XlRowCol ChartDataType)        //插入图表操作
        {
            ChartDataType = Excel.XlRowCol.xlColumns;
            wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            {
                wb.ActiveChart.ChartType = ChartType;
                wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
                wb.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, ws);
            }
        }

        /// <summary>
        /// 保存文档
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            if (mFilename == "")
            {
                return false;
            }
            else
            {
                try
                {
                    wb.Save();
                    return true;
                }

                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 文档另存为
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public bool SaveAs(object FileName)
        {
            try
            {
                wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                return true;

            }

            catch (Exception ex)
            {
                return false;

            }
        }

        /// <summary>
        /// 关闭一个Excel对象，销毁对象
        /// </summary>
        public void Close()
        {
            //wb.Save();            
            wb.Close(Type.Missing, Type.Missing, Type.Missing);
            wbs.Close();
            app.Quit();
            rng = null;
            wb = null;
            wbs = null;
            killApp();
            GC.Collect();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
        }

        #region IDisposable 成员

        public void Dispose()
        {
            if (rng != null)
                rng = null;
            if (wb != null)
                wb = null;
            if (wbs != null)
                wbs = null;
            killApp();
            GC.Collect();
        }

        private void killApp()
        {
            if (app != null)
            {
                app.Quit();

                IntPtr t = new IntPtr(app.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();

                app = null;
            }
        }

        #endregion
    }

    /// <SUMMARY>
    /// ExcelEdit 的摘要说明 //引入Excel的COM组件
    /// </SUMMARY>
    public class ExcelCommon2 : IDisposable
    {
        //打印操作，套打、打印、预览
        public enum PrintFlag
        {
            /// <summary>
            /// 套打，只打印没有印刷的部分
            /// </summary>
            CasePrint,
            /// <summary>
            /// 打印全部
            /// </summary>
            PrintAll,
            /// <summary>
            /// 预览全部
            /// </summary>
            PreviewAll
        }



        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out   int ID);

        public string mFilename;
        public Excel.Application app;
        public Excel.Workbooks wbs;
        public Excel.Workbook wb;
        public Excel.Worksheets wss;
        public Excel.Worksheet ws;
        public Excel.Range rng;

        public string mSheetName;
        public ExcelCommon2()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        /// <summary>
        /// DatagridView导出到Excel且实时打开
        /// </summary>
        /// <param name="sheetName"></param>
        /// <param name="dgv"></param>
        /// <param name="dt"></param>
        public void ExportDgvToExcel(string sheetName, DataGridView dgv, DataTable dt)
        {
            try
            {
                mSheetName = sheetName;
                if (dt != null)
                {
                    this.Create();
                    this.ws = this.AddSheet(mSheetName);

                    this.SetCellValue(this.ws, 1, 1, mSheetName);///row1
                    this.UniteCells(this.ws, 1, 1, 1, dt.Columns.Count);
                    this.SetCellProperty(this.ws, 1, 1, 1, dt.Columns.Count, 16, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);
                    this.SetCellValue(this.ws, 2, 1, "制作日期：" + DateTime.Now.ToString("yyyy-MM-dd"));///row1
                    this.UniteCells(this.ws, 2, 1, 2, dt.Columns.Count);
                    this.SetCellProperty(this.ws, 2, 1, 2, dt.Columns.Count, 11, false, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlRight, false);

                    for (int c = 0; c < dgv.Columns.Count; c++)
                    {
                        this.SetCellValue(this.ws, 3, c + 1, dgv.Columns[c].HeaderText);
                        dt.Columns[dgv.Columns[c].DataPropertyName].SetOrdinal(c);
                    }
                    this.SetCellProperty(this.ws, 3, 1, 3, dgv.Columns.Count, 11, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);
                    this.AddTable(dt, this.ws, 4, 1);

                    this.SetRangeGrid(this.ws, 3, 1, 3 + dt.Rows.Count, dt.Columns.Count, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);
                    this.app.Visible = true;
                    this.ColumnsAutoFit(this.ws, 1, 1, 3 + dt.Rows.Count, dt.Columns.Count);
                }
                //excel.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Dispose();
            }
            finally
            {

            }
        }

        public class ExlCell
        {
            public ExlCell(string cellValue, int cellStartX, int cellStartY, int cellEndX, int cellEndY, ExlCellStyle cellStyle)
            {
                this.CellEndX = cellEndX;
                this.CellEndY = cellEndY;
                this.CellStartX = cellStartX;
                this.CellStartY = cellStartY;
                this.CellStyle = cellStyle;
                this.CellValue = cellValue;
            }

            /// <summary>
            /// 单元格值
            /// </summary>
            public string CellValue { get; set; }
            /// <summary>
            /// 单元格起始行标
            /// </summary>
            public int CellStartX { get; set; }
            /// <summary>
            /// 单元格起始列标
            /// </summary>
            public int CellStartY { get; set; }
            /// <summary>
            /// 单元格结束行标
            /// </summary>
            public int CellEndX { get; set; }
            /// <summary>
            /// 单元格结束列标
            /// </summary>
            public int CellEndY { get; set; }

            public ExlCellStyle CellStyle { get; set; }
        }

        public class ExlCellStyle
        {
            public ExlCellStyle(string fontName, int fontSize, bool bold, Microsoft.Office.Interop.Excel.Constants fontColor, Microsoft.Office.Interop.Excel.Constants fontHorizontalAlignment, bool wrapText)
            {
                this.FontName = fontName;
                this.Bold = bold;
                this.FontColor = fontColor;
                this.FontHorizontalAlignment = fontHorizontalAlignment;
                this.FontSize = fontSize;
                this.WrapText = wrapText;
            }
            /// <summary>
            /// 字体名称如：宋体
            /// </summary>
            public string FontName { get; set; }
            /// <summary>
            /// 字体大小如：11
            /// </summary>
            public int FontSize { get; set; }
            /// <summary>
            /// 字体是否粗体
            /// </summary>
            public bool Bold { get; set; }
            /// <summary>
            /// 字体颜色如： Microsoft.Office.Interop.Excel.Constants.xlColor3
            /// </summary>
            public Microsoft.Office.Interop.Excel.Constants FontColor { get; set; }
            /// <summary>
            /// 字体对齐方式如：Microsoft.Office.Interop.Excel.Constants.xlCenter
            /// </summary>
            public Microsoft.Office.Interop.Excel.Constants FontHorizontalAlignment { get; set; }
            /// <summary>
            /// 字体越界截取
            /// </summary>
            public bool WrapText { get; set; }

        }

        int mHeaderCount = 0;
        int mFooterCount = 0;
        /// <summary>
        /// 为Excel表增加表头
        /// </summary>
        /// <param name="cell"></param>
        void addExlCell(ExlCell cell, ref int counter)
        {
            counter++;
            this.SetCellValue(this.ws, cell.CellStartX, cell.CellStartY, cell.CellValue);
            this.UniteCells(this.ws, cell.CellStartX, cell.CellStartY, cell.CellEndX, cell.CellEndY);
            if (cell.CellStyle != null)
                this.SetCellProperty(this.ws, cell.CellStartX, cell.CellStartY, cell.CellEndX, cell.CellEndY, cell.CellStyle.FontSize, cell.CellStyle.Bold, cell.CellStyle.FontName, cell.CellStyle.FontColor, cell.CellStyle.FontHorizontalAlignment, cell.CellStyle.WrapText);
        }

        /// <summary>
        /// DataTable导出到Excel且实时打开
        /// </summary>
        /// <param name="sheetName"></param>
        /// <param name="dgv"></param>
        /// <param name="dt"></param>
        public void ExportDataTableToExcel(string sheetName, DataTable dt, List<ExlCell> headerCells, List<ExlCell> footerCells)
        {
            try
            {
                mSheetName = sheetName;
                if (dt != null)
                {
                    this.Create();
                    this.ws = this.AddSheet(mSheetName);

                    if (headerCells != null)
                    {
                        foreach (ExlCell el in headerCells)
                            addExlCell(el, ref this.mHeaderCount);
                    }

                    for (int c = 0; c < dt.Columns.Count; c++)
                    {
                        this.SetCellValue(this.ws, mHeaderCount + 1, c + 1, dt.Columns[c].ColumnName);
                    }

                    this.SetCellProperty(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1, dt.Columns.Count, 11, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);
                    this.AddTable(dt, this.ws, mHeaderCount + 2, 1);

                    if (footerCells != null)
                    {
                        foreach (ExlCell el in footerCells)
                            addExlCell(el, ref this.mFooterCount);
                    }

                    this.SetRangeGrid(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1 + dt.Rows.Count, dt.Columns.Count, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);
                    this.app.DisplayAlerts = false;
                    this.ColumnsAutoFit(this.ws, 1, 1, mHeaderCount + mFooterCount + 1 + dt.Rows.Count, dt.Columns.Count);
                    this.app.Visible = true;

                }
                //excel.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Dispose();
            }
            finally
            {

            }
        }

        /// <summary>
        /// DataGridView导出到Excel且实时打开
        /// </summary>
        /// <param name="sheetName"></param>
        /// <param name="dgv"></param>
        /// <param name="dt"></param>
        public void ExportDataGridViewToExcel(string sheetName, DataGridView dgv, List<ExlCell> headerCells, List<ExlCell> footerCells)
        {
            try
            {
                mSheetName = sheetName;
                if (dgv != null)
                {
                    this.Create();
                    this.ws = this.AddSheet(mSheetName);

                    if (headerCells != null)
                    {
                        foreach (ExlCell el in headerCells)
                            addExlCell(el, ref this.mHeaderCount);
                    }

                    int cols = 0;
                    for (int c = 0; c < dgv.Columns.Count; c++)
                    {
                        if (dgv.Columns[c].Visible)
                        {
                            cols++;
                            this.SetCellValue(this.ws, mHeaderCount + 1, cols, dgv.Columns[c].HeaderText);
                        }
                    }

                    this.SetCellProperty(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1, cols, 11, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);
                    this.AddTable(dgv, this.ws, mHeaderCount + 2, 1);

                    if (footerCells != null)
                    {
                        foreach (ExlCell el in footerCells)
                            addExlCell(el, ref this.mFooterCount);
                    }

                    this.SetRangeGrid(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1 + dgv.RowCount, cols, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);
                    this.app.DisplayAlerts = false;
                    this.ColumnsAutoFit(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1 + dgv.RowCount, cols);
                    this.app.Visible = true;

                }
                //excel.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Dispose();
            }
            finally
            {

            }
        }

        public void Print(string sheetName, DataTable dt, List<ExlCell> headerCells, PrintFlag p_printFlag, Excel.XlPaperSize pageSize, int zoom)
        {

            //制作步骤：
            /*    1、用Excel作出与要打印的样式一样的电子表格存为模板；
             *     技巧：最好把第一行与第一列作为空行，以利于调整边距（虽然Excel、打印机可调整页边距）， 尽量的在需要调整的地方多空几行与几列，以利于调整套打对准
             * 
             *  2、如同本程序一样，将Excel作为套打的模板，直接将要打印的数据写入；
             * 
             *  3、打印，根据实际的效果调整Excel模板行高列宽及空出的行列， 直到能够准确的套上。将模板拷贝一份，清除模板上的文字与网格线，做成套打的模板。
             */

            try
            {
                mSheetName = sheetName;
                if (dt != null)
                {
                    this.Create();
                    this.ws = this.AddSheet(mSheetName);

                    if (headerCells != null)
                    {
                        foreach (ExlCell el in headerCells)
                            addExlCell(el, ref this.mHeaderCount);
                    }

                    for (int c = 0; c < dt.Columns.Count; c++)
                    {
                        this.SetCellValue(this.ws, mHeaderCount + 1, c + 1, dt.Columns[c].ColumnName);
                    }

                    this.SetCellProperty(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1, dt.Columns.Count, 11, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);
                    this.AddTable(dt, this.ws, mHeaderCount + 2, 1);

                    this.SetRangeGrid(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1 + dt.Rows.Count, dt.Columns.Count, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);
                    this.app.DisplayAlerts = false;
                    this.ColumnsAutoFit(this.ws, mHeaderCount + 1, 1, mHeaderCount + 1 + dt.Rows.Count, dt.Columns.Count);

                    #region 套打、打印预览

                    this.ws.PageSetup.PaperSize = pageSize;//(Excel.XlPaperSize)Enum.Parse(typeof(Excel.XlPaperSize), "A4");
                    this.ws.PageSetup.Zoom = zoom;//"50%";

                    //刷新Excel屏幕
                    this.app.ScreenUpdating = true;
                    if (p_printFlag == PrintFlag.CasePrint || p_printFlag == PrintFlag.PrintAll)
                    {

                        this.app.Visible = false;
                        this.ws.PrintOut(1, this.wbs.Count, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);//打印
                    }
                    else
                    {
                        this.app.Visible = true;
                        this.ws.PrintPreview(Type.Missing);//预览
                    }
                    this.Close();//关闭并释放    

                    #endregion

                }
                //excel.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Dispose();
            }
            finally
            {

            }

        }

        /// <summary>
        /// 创建一个Excel对象
        /// </summary>
        public void Create()
        {
            app = new Excel.Application();
            wbs = app.Workbooks;
            wb = wbs.Add(true);
        }

        /// <summary>
        /// 打开一个Excel文件
        /// </summary>
        /// <param name="FileName"></param>
        public void Open(string FileName)
        {
            app = new Excel.Application();
            wbs = app.Workbooks;
            wb = wbs.Add(FileName);
            //wb = wbs.Open(FileName, 0, true, 5,"", "", true, Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);
            //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
            mFilename = FileName;
        }

        /// <summary>
        /// 获取一个工作表
        /// </summary>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet GetSheet(string SheetName)
        {
            Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[SheetName];
            return s;
        }

        /// <summary>
        /// 添加一个工作表
        /// </summary>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet AddSheet(string SheetName)
        {
            Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            s.Name = SheetName;
            return s;
        }

        /// <summary>
        /// 删除一个工作表
        /// </summary>
        /// <param name="SheetName"></param>
        public void DelSheet(string SheetName)
        {
            ((Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
        }

        /// <summary>
        /// 重命名一个工作表
        /// </summary>
        /// <param name="OldSheetName"></param>
        /// <param name="NewSheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)
        {
            Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[OldSheetName];
            s.Name = NewSheetName;
            return s;
        }

        /// <summary>
        /// 重命名一个工作表
        /// </summary>
        /// <param name="Sheet"></param>
        /// <param name="NewSheetName"></param>
        /// <returns></returns>
        public Excel.Worksheet ReNameSheet(Excel.Worksheet Sheet, string NewSheetName)
        {

            Sheet.Name = NewSheetName;

            return Sheet;
        }

        /// <summary>
        /// 要设值的工作表的名称 X行Y列 value 值
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void SetCellValue(Excel.Worksheet ws, int x, int y, object value)
        {
            ws.Cells[x, y] = value;
        }

        /// <summary>
        /// 要设值的工作表的名称 X行Y列 value 值
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void SetCellValue(string ws, int x, int y, object value)
        {

            GetSheet(ws).Cells[x, y] = value;
        }

        /// <summary>
        /// 设置一个单元格的属性 字体，粗体，大小，颜色，对齐方式
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="Startx"></param>
        /// <param name="Starty"></param>
        /// <param name="Endx"></param>
        /// <param name="Endy"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="color"></param>
        /// <param name="HorizontalAlignment"></param>
        public void SetCellProperty(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, bool bold, string name, Excel.Constants color, Excel.Constants HorizontalAlignment, bool wrapText)
        {
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Bold = bold;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).WrapText = wrapText;
        }


        /// <summary>
        /// 设置一个单元格的属性 字体，大小，颜色，对齐方式
        /// </summary>
        /// <param name="wsn"></param>
        /// <param name="Startx"></param>
        /// <param name="Starty"></param>
        /// <param name="Endx"></param>
        /// <param name="Endy"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="color"></param>
        /// <param name="HorizontalAlignment"></param>
        public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)
        {
            //name = "宋体";
            //size = 12;
            //color = Excel.Constants.xlAutomatic;
            //HorizontalAlignment = Excel.Constants.xlRight;

            Excel.Worksheet ws = GetSheet(wsn);
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;

            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
        }

        /// <summary>
        /// 设置区域网格
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="Startx"></param>
        /// <param name="Starty"></param>
        /// <param name="Endx"></param>
        /// <param name="Endy"></param>
        /// <param name="lineSyle"></param>
        /// <param name="borderWeight"></param>
        /// <param name="colorIndex"></param>
        public void SetRangeGrid(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, Excel.XlLineStyle lineSyle, Excel.XlBorderWeight borderWeight, Excel.XlColorIndex colorIndex)
        {
            rng = ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]);
            rng.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            rng.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone;

            rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeTop].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = colorIndex;

            rng.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = lineSyle;
            rng.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = borderWeight;
            rng.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = colorIndex;
        }

        /// <summary>
        /// 指定单元格宽度自动适应
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public void ColumnsAutoFit(Excel.Worksheet ws, int x1, int y1, int x2, int y2)
        {
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Select();
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Columns.AutoFit();
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Rows.AutoFit();
        }

        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public void UniteCells(Excel.Worksheet ws, int x1, int y1, int x2, int y2)
        {
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
        }


        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public void UniteCells(string ws, int x1, int y1, int x2, int y2)
        {
            GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);

        }


        /// <summary>
        /// 将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
        {

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    GetSheet(ws).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();

                }

            }

        }

        /// <summary>
        /// 将内存中数据表格插入到Excel指定工作表的指定位置二
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void InsertTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY)
        {

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {

                    ws.Cells[startX + i, j + startY] = dt.Rows[i][j];

                }

            }

        }


        /// <summary>
        /// 将内存中数据表格添加到Excel指定工作表的指定位置一
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
        {

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {

                    GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];

                }

            }

        }

        /// <summary>
        /// 将内存中数据表格添加到Excel指定工作表的指定位置二
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ws"></param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public void AddTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY)
        {


            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {

                    ws.Cells[i + startX, j + startY] = dt.Rows[i][j];

                }
            }

        }

        public void AddTable(DataGridView dgv, Excel.Worksheet ws, int startX, int startY)
        {
            for (int r = 0; r <= dgv.RowCount - 1; r++)
            {
                int cols = 0;
                for (int l = 0; l < dgv.Columns.Count; l++)
                {
                    if (dgv.Columns[l].Visible)
                    {
                        ws.Cells[r + startX, cols + startY] = dgv[l, r].Value;
                        cols++;
                    }
                }
            }
        }

        /// <summary>
        /// 插入图片操作
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="ws"></param>
        public void InsertPictures(string Filename, string ws)
        {
            //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
            //后面的数字表示位置
        }

        /// <summary>
        /// 插入图片操作
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="ws"></param>
        /// <param name="Height"></param>
        /// <param name="Width"></param>
        public void InsertPictures(string Filename, string ws, int Height, int Width)
        {
            //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
        }

        /// <summary>
        /// 插入图片操作
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="ws"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="Height"></param>
        /// <param name="Width"></param>
        public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)
        {

            //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
            //GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
        }

        public void InsertActiveChart(Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Excel.XlRowCol ChartDataType)        //插入图表操作
        {
            ChartDataType = Excel.XlRowCol.xlColumns;
            wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            {
                wb.ActiveChart.ChartType = ChartType;
                wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
                wb.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, ws);
            }
        }

        /// <summary>
        /// 保存文档
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            if (mFilename == "")
            {
                return false;
            }
            else
            {
                try
                {
                    wb.Save();
                    return true;
                }

                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 文档另存为
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public bool SaveAs(object FileName)
        {
            try
            {
                wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                return true;

            }

            catch (Exception ex)
            {
                return false;

            }
        }

        /// <summary>
        /// 关闭一个Excel对象，销毁对象
        /// </summary>
        public void Close()
        {
            //wb.Save();            
            wb.Close(Type.Missing, Type.Missing, Type.Missing);
            wbs.Close();
            app.Quit();
            rng = null;
            wb = null;
            wbs = null;
            killApp();
            GC.Collect();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
        }

        #region IDisposable 成员

        public void Dispose()
        {
            if (rng != null)
                rng = null;
            if (wb != null)
                wb = null;
            if (wbs != null)
                wbs = null;
            killApp();
            GC.Collect();
        }

        private void killApp()
        {
            if (app != null)
            {
                app.Quit();

                IntPtr t = new IntPtr(app.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();

                app = null;
            }
        }

        #endregion
    }
    #endregion
}
